﻿using System;
using System.Windows.Forms;
using Me.Amon.C;
using Me.Amon.POS.M;
using Me.Amon.Properties;
using Me.Amon.Util;
using Newtonsoft.Json;

namespace Me.Amon.POS.V
{
    public partial class UcLogin : UserControl, IAuth
    {
        private Main _Main;

        public UcLogin()
        {
            InitializeComponent();
        }

        public Control Control
        {
            get { return this; }
        }

        public void Init(Main main)
        {
            _Main = main;

            _Main.ShowTips(PbCfg, "系统配置");
            _Main.AcceptButton = BtUpdate;
            _Main.CancelButton = BtCancel;
        }

        private void BtUpdate_Click(object sender, EventArgs e)
        {
            var user = TbUser.Text.Trim();
            if (string.IsNullOrEmpty(user))
            {
                MessageBox.Show("请输入用户！");
                TbUser.Focus();
                return;
            }
            if (!CharUtil.IsValidate(user, 3, 32))
            {
                MessageBox.Show("用户格式输入错误！");
                TbPass.Text = "";
                TbUser.Focus();
                return;
            }

            var pass = TbPass.Text.Trim();
            if (string.IsNullOrEmpty(pass))
            {
                MessageBox.Show("请输入密码！");
                TbUser.Focus();
                return;
            }
            TbPass.Text = "";
            if (!CharUtil.IsValidate(pass, 3, 32))
            {
                MessageBox.Show("密码格式输入错误！");
                TbPass.Focus();
                return;
            }

            var setting = Settings.Default;

            // 首次登录
            if (string.IsNullOrWhiteSpace(setting.Server))
            {
                MessageBox.Show("系统配置错误，请与管理员联系！");
                return;
            }

            // 正常登录
            var data = string.Format("{0}/POS/Emp.ashx?t={1}&{2}={3}&{4}={5}&{6}={7}&{8}={9}",
                setting.Server, setting.Code,
                PPos.PARAM_KEY_OPT, "l",
                PPos.PARAM_KEY_CORP, setting.CorpCode,
                PPos.PARAM_KEY_SHOP, setting.ShopCode,
                PPos.PARAM_KEY_USER, user);

            try
            {
                var rest = WebUtil.POST(data, PPos.PARAM_KEY_DATA + '=' + HashUtil.Password(pass));
                if (string.IsNullOrWhiteSpace(rest))
                {
                    return;
                }

                var emp = JsonConvert.DeserializeObject<MEmp>(rest);
                if (emp.HasError())
                {
                    MessageBox.Show(emp.GetError());
                    return;
                }
                new WPos(_Main, emp).Show();
                _Main.Visible = false;
            }
            catch (Exception exp)
            {
                MessageBox.Show("系统登录异常，请与管理员联系！" + exp.Message);
            }
        }

        private void BtCancel_Click(object sender, EventArgs e)
        {
            _Main.Close();
        }

        private void PbCfg_Click(object sender, EventArgs e)
        {
            _Main.ShowSetup();
        }
    }
}
